<?php

/**
 *
 * PHP API 商品规格控制器
 * @author jeff
 * powered by jeff liu 2011-06-07
 */
class Goods_specApp extends ApiApp
{
    public $_goods_spec_mod;

    function __construct()
    {
        $this->Goods_specApp();
    }

    function Goods_specApp()
    {
        parent::__construct();
        $this->_goods_spec_mod =& api_model('goodsspec');
    }

    /**
     * 获得商品某个规格的详细信息
     * powered by jeff 2011-6-8
     */
    function apiGetProductDetail()
    {
        $result = $this->_getProductFieldBySpecId('*');
        echo $result;
    }

    /**
     * 获得商品所有规格的详细信息
     * powered by jeff 2011-6-8
     */
    function apiGetProductListByGoodsId()
    {
        $goods_id = $_REQUEST['id'];
        if (empty($goods_id)) {
            $error = ApiError::showError('goods_id_required', 1);
            echo $error;
            return;
        }
        $field = '*';
        $sql = sprintf(
            'SELECT %s.%s FROM %s %s WHERE %s.goods_id=%.0f',
            $this->_goods_spec_mod->alias,
            $field,
            $this->_goods_spec_mod->table,
            $this->_goods_spec_mod->alias,
            $this->_goods_spec_mod->alias,
            (float)$goods_id
        );
        $result = $this->_goods_spec_mod->getAll($sql);
        $result = array('data' => $result);
        $this->echo_result($result);
    }

    /**
     * 获得商品某个规格的价格
     * powered by jeff 2011-6-8
     */
    function apiGetProductPrice()
    {
        $result = $this->_getProductFieldBySpecId('price');
        echo $result;
    }

    /**
     * 获得商品某个规格的库存量
     * powered by jeff 2011-6-8
     */
    function apiGetProductStock()
    {
        $result = $this->_getProductFieldBySpecId('stock');
        echo $result;
    }

    /**
     * 获得商品某个规格的sku
     * powered by jeff 2011-6-8
     */
    function apiGetProductSku()
    {
        $result = $this->_getProductFieldBySpecId('sku');
        echo $result;
    }

    /**
     * 获得商品某个规格的建议销售价
     * powered by jeff 2011-6-8
     */
    function apiGetProductSuggestPrice()
    {
        $result = $this->_getProductFieldBySpecId('suggest_price');
        echo $result;
    }

    /**
     * 获得商品某个规格的规格值（spec_1,spec_2,color_rgb）
     * powered by jeff 2011-6-8
     */
    function apiGetProductSpec()
    {
        $spec_id = $_REQUEST['id'];
        if (empty($spec_id)) {
            $error = ApiError::showError('spec_id_required', 1);
            echo $error;
            return;
        }

        $sql = sprintf(
            'SELECT %s.spec_1, %s.spec_2, %s.color_rgb FROM %s %s WHERE %s.spec_id=%.0f',
            $this->_goods_spec_mod->alias,
            $this->_goods_spec_mod->alias,
            $this->_goods_spec_mod->alias,
            $this->_goods_spec_mod->table,
            $this->_goods_spec_mod->alias,
            $this->_goods_spec_mod->alias,
            (float)$spec_id
        );
        $result = $this->_goods_spec_mod->getOne($sql);
        $result = array('data' => $result);
        $this->echo_result($result);
    }

    /**
     * 获得商品信息的公共函数
     * powered by jeff 2011-6-8
     * @param string $field
     */
    private function _getProductFieldBySpecId($field = '')
    {
        $spec_id = $_REQUEST['id'];
        if (empty($spec_id)) {
            $error = ApiError::showError('spec_id_required', 1);
            echo $error;
            return;
        }

        $sql = sprintf(
            'SELECT %s.%s FROM %s %s WHERE %s.spec_id=%.0f',
            $this->_goods_spec_mod->alias,
            $field,
            $this->_goods_spec_mod->table,
            $this->_goods_spec_mod->alias,
            $this->_goods_spec_mod->alias,
            (float)$spec_id
        );
        $result = $this->_goods_spec_mod->getOne($sql);
        $result = array('data' => $result);
        $result = $this->return_result($result);
        return $result;
    }
}